﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;

namespace HSE
{
    public partial class Default : UserControl
    {
        public DefaultViewModel viewmodel;
        public DispatcherTimer _timer1;
        public DispatcherTimer _timer2;
        int refeshCycle = 300;
        public Default()
        {
            InitializeComponent();
            viewmodel = new DefaultViewModel();
            this.DataContext = viewmodel;
            _timer1 = new DispatcherTimer();
            _timer1.Tick += new EventHandler(_timer_Tick1);
            _timer1.Interval = new TimeSpan(0, 0, refeshCycle);

            _timer2 = new DispatcherTimer();
            _timer2.Tick += new EventHandler(_timer_Tick2);
            _timer2.Interval = new TimeSpan(0, 0, 1);
            _timer1.Start();
            _timer2.Start();
        }

        void _timer_Tick1(object sender, EventArgs e)
        {
            _timer2.Stop();
            _timer1.Stop();
            refeshCycle = 300;
            _timer1.Interval = new TimeSpan(0, 0, refeshCycle);
            _timer1.Start();
            _timer2.Start();
            viewmodel.Messages.Insert(0,new Message() { Notice = "最新通知：HSE系统1月1日全面投入使用！" });
        }

        void _timer_Tick2(object sender, EventArgs e)
        {
            refeshCycle--;
            viewmodel.TipInfo = string.Format("剩余 {0} 秒更新获取通知！", refeshCycle);
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Image img = (Image)sender;
            viewmodel.Messages.Remove(img.Tag as Message);
        }
    }
}
